var submitType = 0
layui.use(['table', 'admin', 'ax', 'ztree', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var $ZTree = layui.ztree;
    var form = layui.form;

    /**
     * 单位分组管理
     */
    var HxBasicUnitCode = {
        tableId: "hxBasicUnitCodeTable",
        condition: {
            id: ""
        }
    };

    /**
     * 选择编码时
     */
    HxBasicUnitCode.onClickHxBasicUnitCode = function (e, treeId, treeNode) {
        HxBasicUnitCode.condition.id = treeNode.id;

		$("#legend").text("修改编码");
	    //获取详情信息，填充表单
	    var ajax = new $ax(Feng.ctxPath + "/hxBasicUnitCode/detail?id=" + treeNode.id);
	    var result = ajax.start();
	    form.val('hxBasicUnitCodeForm', result.data);
		$("#butshow").show();
		submitType = 2
    };

	// 同步门店分类信息
	$('#pickProducts').click(function () {
		layer.open({
			type: 2,
			title: '同步门店分类信息',
			area: ['1000px', '750px'],
			anim: 5,
			content: Feng.ctxPath + '/hxBasicUnitCode/pickProducts',
			end: function () {
			    //初始化左侧部门树
			    var ztree = new $ZTree("codeTree", "/hxBasicUnitCode/tree?v1="+$("#v1").val());
			    ztree.bindOnClick(HxBasicUnitCode.onClickHxBasicUnitCode);
			    ztree.init();
			}
		});
	});

    /**
     * 点击添加
     */
    $('#btnAdd').click(function () {
		$("#legend").text("添加分类");
		$("#hxBasicUnitCodeForm")[0].reset();
        layui.form.render();
		if(HxBasicUnitCode.condition.id == ""){
			$("#pid").val("0");
		} else {
			$("#pid").val(HxBasicUnitCode.condition.id);
		}
		$("#butshow").show();
		submitType = 1
    });

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
		if(submitType == 1){
	        var ajax = new $ax(Feng.ctxPath + "/hxBasicUnitCode/addItem", function (data) {
	            Feng.success("添加成功！");
				$("#hxBasicUnitCodeForm")[0].reset();
		        layui.form.render();
				if(HxBasicUnitCode.condition.id == ""){
					$("#pid").val("0");
				} else {
					$("#pid").val(HxBasicUnitCode.condition.id);
				}
			    var ztree = new $ZTree("codeTree", "/hxBasicUnitCode/tree?v1="+$("#v1").val());
			    ztree.bindOnClick(HxBasicUnitCode.onClickHxBasicUnitCode);
			    ztree.init();
	        }, function (data) {
	            Feng.error("添加失败！" + data.responseJSON.message)
	        });
	        data.field.v1 = $("#v1").val()
	        ajax.set(data.field);
	        ajax.start();
		} 
		if(submitType == 2){
	        var ajax = new $ax(Feng.ctxPath + "/hxBasicUnitCode/editItem", function (data) {
	            Feng.success("更新成功！");
			    var ztree = new $ZTree("codeTree", "/hxBasicUnitCode/tree?v1="+$("#v1").val());
			    ztree.bindOnClick(HxBasicUnitCode.onClickHxBasicUnitCode);
			    ztree.init();
	        }, function (data) {
	            Feng.error("更新失败！" + data.responseJSON.message)
	        });
	        ajax.set(data.field);
	        ajax.start();
		} 

        return false;
    });

    /**
     * 点击删除
     */
    $('#delete').click(function () {
		if(submitType == 1){
			$("#hxBasicGoodsCodeForm")[0].reset();
	        layui.form.render();
			if(HxBasicUnitCode.condition.id == ""){
				$("#pid").val("0");
			} else {
				$("#pid").val(HxBasicUnitCode.condition.id);
			}
		}
		if(submitType == 2){
	        var operation = function () {
	            var ajax = new $ax(Feng.ctxPath + "/hxBasicUnitCode/delete", function (data) {
	                Feng.success("删除成功!");
					$("#butshow").hide();
					$("#hxBasicUnitCodeForm")[0].reset();
	                layui.form.render();
				    var ztree = new $ZTree("codeTree", "/hxBasicUnitCode/tree?v1="+$("#v1").val());
				    ztree.bindOnClick(HxBasicUnitCode.onClickHxBasicUnitCode);
				    ztree.init();
	            }, function (data) {
	                Feng.error("删除失败!" + data.responseJSON.message + "!");
	            });
	            ajax.set("id", HxBasicUnitCode.condition.id);
	            ajax.start();
	        };
	        Feng.confirm("是否删除?", operation);
		}
    });

    //初始化左侧部门树
    var ztree = new $ZTree("codeTree", "/hxBasicUnitCode/tree?v1="+$("#v1").val());
    ztree.bindOnClick(HxBasicUnitCode.onClickHxBasicUnitCode);
    ztree.init();

	//选择单位类型时
    form.on('select(v1)',function(){
	    //初始化左侧部门树
	    var ztree = new $ZTree("codeTree", "/hxBasicUnitCode/tree?v1="+$("#v1").val());
	    ztree.bindOnClick(HxBasicUnitCode.onClickHxBasicUnitCode);
	    ztree.init();
	    
		$("#hxBasicGoodsCodeForm")[0].reset();
        layui.form.render();
		if(HxBasicUnitCode.condition.id == ""){
			$("#pid").val("0");
		} else {
			$("#pid").val(HxBasicUnitCode.condition.id);
		}

    	form.render('select');
    });

});
